package com.system.borrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;

import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.model.HttpParams;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SecondTitle;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import okhttp3.Request;
import okhttp3.Response;

public class WebViewActivity extends BaseActivity {
    WebView webView;
    String url;
    int type = 0;
    String name;
    int id = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        type = intent.getIntExtra("type",0);
        id = intent.getIntExtra("id",0);
        name = intent.getStringExtra("name");
        SecondTitle title = new SecondTitle(this);
        title.setTitle(name,null);
        if(type!=0){
            addLook();
        }
        initView();
        addAction();
    }
    private void addLook() {
        HttpParams params = new HttpParams();
        params.put("dataId",id+"");
        params.put("flag","2");
        OkHttpUtils.post(URLDefind.ADD_LLOK)
                .tag(this)
                .params(params)
                .execute(new DialogCallback<String>(this,String.class,null) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                    }
                });

    }

    private void addAction() {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    private void initView() {
        webView = (WebView) findViewById(R.id.webView);
        Log.v("url======",url);
        webView.loadUrl(url);
    }
}
